package com.leviassaf.androidbasics;

import android.R.string;
import android.app.Activity;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.preference.PreferenceManager;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.RadioGroup;
import android.widget.RadioGroup.OnCheckedChangeListener;
import android.widget.TextView;

//45
public class OpenedClass extends Activity implements OnClickListener, OnCheckedChangeListener {
	
	//46
	TextView txtPersonIs;
	TextView txtMessage;
	Button btnReturn;
	RadioGroup radgrpAnswers;
	//48
	String strTextPassedFromCallingClass;
	//49
	String strDiagnostic;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		
		String strTextCombinedFromSharedPrefsAndTextSentFromCallingClass = null;
		
		setContentView(R.layout.send);
		setJavaRefs();
		
		//58
		SharedPreferences sharedPrefs= PreferenceManager.getDefaultSharedPreferences(getBaseContext());
		String strFullName = sharedPrefs.getString("prefedtFullName", "Some Default Text");
		String strPreferredActivities = sharedPrefs.getString("list", "itmOther");
		if (strPreferredActivities.contentEquals("itmSport")){
			strTextCombinedFromSharedPrefsAndTextSentFromCallingClass = String.format("Text from shared preferences is '%s'", strFullName);			
		}
		
		//48
		//50
		Bundle bndl = getIntent().getExtras();
		strTextPassedFromCallingClass = bndl.getString("key");
		strTextCombinedFromSharedPrefsAndTextSentFromCallingClass = 
				String.format("%s\nText passed from calling class is: '%s'", strTextCombinedFromSharedPrefsAndTextSentFromCallingClass, strTextPassedFromCallingClass);
		txtPersonIs.setText(strTextCombinedFromSharedPrefsAndTextSentFromCallingClass);

		this.setTitle(String.format("%s-%s", this.getTitle().toString(), this.getClass().getSimpleName()));
	}

	private void setJavaRefs() {
		// TODO Auto-generated method stub
		txtPersonIs = (TextView)findViewById(R.id.txtPersonIs);
		txtMessage = (TextView)findViewById(R.id.txtMessage);
		btnReturn = (Button)findViewById(R.id.btnReturn);
		radgrpAnswers = (RadioGroup)findViewById(R.id.radgrpAnswers);
		btnReturn.setOnClickListener(this);
		radgrpAnswers.setOnCheckedChangeListener(this);
	}

	//50
	@Override
	public void onClick(View v) {
		// TODO Auto-generated method stub
		Intent intent = new Intent();
		Bundle bndl = new Bundle();
		bndl.putString("answer", strDiagnostic);
		intent.putExtras(bndl);
		setResult(RESULT_OK, intent);
		finish();
	}

	@Override
	public void onCheckedChanged(RadioGroup group, int checkedId) {
		// TODO Auto-generated method stub
		switch(checkedId){
		case R.id.radCool:
			strDiagnostic = "Probably right!";
			break;
		case R.id.radCrazy:
			strDiagnostic = "Definitely right!!!!";
			break;
		case R.id.radBoth:
			strDiagnostic = "Spot on!";
			break;
		}
		txtMessage.setText(strDiagnostic);
	}
}
